import java.util.Random;

public final class RandomBoard {

    private static Random randGen = new Random();

    public static void main(String[] args) {
        int nbrBoards = 1;

        if (args.length > 0) {
            try {
                nbrBoards = Integer.parseInt(args[0]);
            } catch (NumberFormatException e) {
                // Leave as default value 1
            }
        }

        for (int i = 0; i < nbrBoards; i++) {
            Board board = RandomBoard.randPieces();
            GameEngine.printBoard(board);
        }
    }

    public static Board randPieces() {

        int nBlacks = 1+randGen.nextInt(12);
        int nWhites = 1+randGen.nextInt(12);

        int black = 0x00000000;
        while ( Integer.bitCount(black) < nBlacks ) {
            black |= ( 1<<randGen.nextInt(32) );
        }

        int white = 0xFFFFFFFF & (~black);
        while ( Integer.bitCount(white) > nWhites ) {
            white &= ~( 1<<randGen.nextInt(32) );
        }

        int dampening = -1;
        for (int i = 0; i < 3; i++) {
            dampening &= randGen.nextInt();
        }
        int kings = (black | white) & dampening;
        kings |= black & Board.BOTTOM_ROW;
        kings |= white & Board.TOP_ROW;

        while ( Integer.bitCount(kings) > Board.MAX_KINGS ) {
            int bitNbr = randGen.nextInt(24) + 4;
            kings &= ~( 1<<randGen.nextInt(bitNbr) );
        }

        boolean blackToMove = randGen.nextBoolean();

        return new Board(black, white, kings, blackToMove);
    }
}
